package com.ooo.sdkdemo.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.ooo.sdkdemo.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.LinkedList;


/**此类为了方便测试 接入SDK无需理会*/
public class MySpinner extends Spinner {


    public MySpinner(Context context, AttributeSet attrs) {
        super(context, attrs);


        LinkedList<String> platformList = new LinkedList<String>();

        try {
            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( context.getAssets().open("platforms.txt" ) ) );
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                platformList.add( str );
            }
        } catch (Exception e) {
            e.printStackTrace();
            platformList.add("99you=nsdkapi.99you.cn");
        }


        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>( context, R.layout.item_dropdown ,platformList.toArray(new String[0]) );
        //设置数组适配器的布局样式
//        starAdapter.setDropDownViewResource(R.layout.item_dropdown);
        //设置下拉框的标题，不设置就没有难看的标题了
        setPrompt("请选择平台");
        //设置下拉框的数组适配器
        setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] platform = platformList.get(i).split("=");
                if( onItemSelectedListener != null ){
                    onItemSelectedListener.onItemSelected( new String[]{platform[0],platform[1]} );
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

    }

    private OnItemSelectedListener onItemSelectedListener;
    public void setOnItemSelectedListener( OnItemSelectedListener onItemSelectedListener ){
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public interface OnItemSelectedListener{
        public void onItemSelected( String[] url );
    }

}
